package com.central.apps.handler.inspcriterion;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.central.app.common.base.BaseHandler;
import com.central.app.common.constant.AppConstant;
import com.central.app.service.HandlerService;
import com.central.apps.dao.inspection.InspplanDao;
import com.central.apps.dao.inspection.inspcontenDao;
import com.central.apps.dao.inspection.inspregionalDao;
import com.central.common.auth.details.LoginAppUser;
import com.central.common.util.SysUserUtil;
import com.central.common.web.PageResult;
import com.central.common.web.Result;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class InspcontentHandler  extends BaseHandler{
        @Autowired
        private inspregionalDao inspregionalDao;
        @Autowired
        private inspcontenDao  inspcontenDao;
        @Autowired
        private HandlerService handlerService;

    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        if ("insptaskApp".equals(app)) {
            if ("getInsaenum".equals(type)){

                String insptasknum= MapUtils.getString(params, "insptasknum");
                Page<Map<String,Object>> page = new Page<>(1, 10);
                if (MapUtils.getInteger(params, AppConstant.BIZ_QUERY_PAGE) != null && MapUtils.getInteger(params, AppConstant.BIZ_QUERY_LIMIT) != null) {
                    page = new Page<>(MapUtil.getInt(params, AppConstant.BIZ_QUERY_PAGE), MapUtil.getInt(params, AppConstant.BIZ_QUERY_LIMIT));
                }
                IPage<Map<String, Object>> pageInfo = inspregionalDao.getregionaHandler(page, insptasknum);
                return PageResult.<Map<String, Object>>builder().data(pageInfo.getRecords()).code(0).count(pageInfo.getTotal()).build();
            }
        }
        return super.operate(app, type, params);

    }

    @Override
        public void beforSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        }

        @Override
        public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {


            LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
            Long id1 = loginAppUser.getId();
            String nickname = loginAppUser.getNickname();
            if ("insptaskApp".equals(app)) {
                if ("gq_insptarea".equals(objName)) {
                    //点检区域选择区域后自动将该区域下的相关资产及点检标准写入到“计划资产”中
                    String id = MapUtils.getString(data, "id");
                    String insaenum = MapUtils.getString(data, "insaenum");
                    //通过区域编号查区域下的  区域子表资产
                    List<Map<String,Object>> bysqlList  = inspcontenDao.getInspplanHandler(insaenum);
                    if (CollectionUtils.isNotEmpty(bysqlList)) {
                        for (int i = 0;i<bysqlList.size();i++){
                            Map<String, Object> assetMap = bysqlList.get(i);
                            String assetnum = MapUtils.getString(assetMap,"assetnum");
                            String assetdesc = MapUtils.getString(assetMap,"assetdesc");
                            String inspnum = MapUtils.getString(assetMap,"inspnum");
                            String inspdesc = MapUtils.getString(assetMap,"inspdesc");
                            Map<String,Object> planassetsMap = new HashMap<>();
                            planassetsMap.put("insptareaid",id);
                            planassetsMap.put("assetnum",assetnum);
                            planassetsMap.put("assetdesc",assetdesc);
                            planassetsMap.put("inspnum",inspnum);
                            planassetsMap.put("inspdesc",inspdesc);

                            String insptstandId = handlerService.insert("gq_insptstand", planassetsMap).toString();
                            //新增计划资产  并且关联计划区域
                            Map<String,Object> contentsMap = new HashMap<>();
                            String inspnumcont = (String) planassetsMap.get("inspnum");
                            List<Map<String,Object>> byList  = inspcontenDao.getIncontentHandler(inspnumcont);
                            if (CollectionUtils.isNotEmpty(byList)) {
                                for (int j = 0;j<byList.size();j++){
                                    Map<String, Object> assettMap = byList.get(j);
                                    String inspcont = MapUtils.getString(assettMap,"inspcont");
                                    String inspstan = MapUtils.getString(assettMap,"inspstan");

                                    contentsMap.put("insptstandid",insptstandId);
                                    contentsMap.put("inspcont",inspcont);
                                    contentsMap.put("inspstan",inspstan);
                                    contentsMap.put("inspperson",id1);
                                    contentsMap.put("inspperdesc",nickname);
                                    contentsMap.put("createtime",new Date());
                                    inspcontenDao.insertt(contentsMap);
                                }

                            }
                        }


                    }

                }
            }
        }

    }

